package com.zego.wawaji.ui.center.wawa;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.log.PToast;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.base.observer.PObserver;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.BasePanel;
import com.zego.wawaji.ui.base.CustomRecyclerView;
import com.zego.wawaji.ui.base.xlistview.ExListView;
import com.zego.wawaji.ui.bean.wawa.MyWawa;
import com.zego.wawaji.ui.bean.wawa.MyWawaList;
import com.zego.wawaji.ui.bean.wawa.WawaConstant;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.dialog.SimpleTipDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的娃娃：寄存中
 * Created by Su on 2017/11/26.
 */
public class MyWawaUntreated extends BasePanel implements PObserver, RequestComplete, ExListView.IXListViewListener, View.OnClickListener {
    private CustomRecyclerView statusLayout;
    private ExListView exListView;
    private TextView sendAll;
    private int page = 1; // 加载页码

    private UntreatedAdapter adapter;
    private List<MyWawa> list = new ArrayList<>();

    public MyWawaUntreated(Context context) {
        super(context);
        setContentView(R.layout.my_wawa_untreated);
        initView();
        onRefresh();
    }

    private void initView() {
        MsgMgr.getInstance().attach(this);
        statusLayout = (CustomRecyclerView) findViewById(R.id.statusLayout);
        View mViewTop = LayoutInflater.from(getContext()).inflate(R.layout.layout_margintop, null);
        exListView = statusLayout.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        exListView.addHeaderView(mViewTop);
        adapter = new UntreatedAdapter(getContext(), list);
        exListView.setAdapter(adapter);
        statusLayout.showLoading();

        sendAll = (TextView) findViewById(R.id.send_all);
        sendAll.setOnClickListener(this);
        sendAll.setVisibility(View.GONE);
    }

    @Override
    public void onRefresh() {
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getWawaMgr().reqMyWawa(WawaConstant.WAWA_NO_SEND, page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getWawaMgr().reqMyWawa(WawaConstant.WAWA_NO_SEND, page, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        exListView.stopRefresh();
        exListView.stopLoadMore();
        if (response.getUrlParam() == UrlParam.reqMyWawa) {
            setData(response);
        }
    }

    private void setData(HttpResponse response) {
        // 请求失败
        if (!response.isOk()) {
            if (list.size() != 0) {
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
                sendAll.setVisibility(View.VISIBLE);
                return;
            }
            statusLayout.showNoData();
            sendAll.setVisibility(View.GONE);
            return;
        }

        // 非缓存
        if (!response.isCache()) {
            MyWawaList myWawaList = (MyWawaList) response.getBaseData();

            if (myWawaList.getMyWawaList().size() != 0) {
                //如果请求的第一页 应该把之前的数据都清掉，把返回的数据作为第一页
                if (page == 1) {
                    if (list.size() != 0) {
                        list.clear();
                    }
                    page = 1;
                }

                list.addAll(myWawaList.getMyWawaList());
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
                sendAll.setVisibility(View.VISIBLE);
            } else {
                if (page == 1) {
                    statusLayout.showNoData();
                    sendAll.setVisibility(View.GONE);
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }
            return;
        }

        // 缓存
        if (page == 1) {
            MyWawaList myWawaList = (MyWawaList) response.getBaseData();

            if (myWawaList != null && myWawaList.getMyWawaList().size() != 0) {
                if (list.size() != 0) {
                    list.clear();
                }
                list.addAll(myWawaList.getMyWawaList());
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
                sendAll.setVisibility(View.VISIBLE);
            }
        }
    }

    public void release() {
        MsgMgr.getInstance().detach(this);
    }

    /**
     * 刷新首页
     */
    public void refreshList() {
        onRefresh();
        if (adapter != null && adapter.getList() != null && adapter.getList().size() != 0) {
            exListView.setSelection(0);
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Wawa_Untreated:
                refreshList();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_all:
                sendAll();
                break;
        }
    }

    private void sendAll() {
        if (list == null || list.size() <= 0) {
            PToast.showShort("您还没有寄存中的娃娃！");
            return;
        }
        String freeSendNum = ModuleMgr.getWawaMgr().getCommon().getFreeSendWwMin();
        String sendWwCoin = ModuleMgr.getWawaMgr().getCommon().getRequsetSendWwCoinMin();
        String num = TextUtils.isEmpty(freeSendNum) ? "4" : freeSendNum;
        String coinNum = TextUtils.isEmpty(sendWwCoin) ? "80" : sendWwCoin;
        if (list.size() < Integer.valueOf(num)) {
            String content = "限时活动满" + num + "件包邮，低于" + num + "件收取" + coinNum + "金币运费哦！";
            PickerDialogUtil.showSimpleTipDialogExt((FragmentActivity) getActivity(), new SimpleTipDialog.ConfirmListener() {
                        @Override
                        public void onCancel() {
                        }

                        @Override
                        public void onSubmit() {
                            UIShow.showMyAddressAct(getActivity(), 1, list.size());
                        }
                    }, content, "温馨提示", "取消", "确认发货",
                    true, R.color.text_gray);
            return;
        }

        UIShow.showMyAddressAct(getActivity(), 1, list.size());
    }
}
